package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.commom.PageResult;
import com.qk.dto.UserDto;
import com.qk.entity.User;
import com.qk.exception.ServiceException;
import com.qk.mapper.UserMapper;
import com.qk.service.UserService;
import com.qk.utils.JwtUtils;
import com.qk.vo.LoginResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Autowired
   private UserMapper userMapper;
    @Override
    public PageResult<User> list(UserDto userDto) {
        PageHelper.startPage(userDto.getPage(),userDto.getPageSize());

        List<User> userList = userMapper.selectList(userDto);

        Page<User> page = (Page<User>) userList;

        return new PageResult<>(page.getTotal(),page.getResult());
    }

    @Override
    public void add(User user) {
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        String password=user.getUsername()+"123";
        user.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
        userMapper.add(user);
    }

    @Override
    public void delete(List<Integer> ids) {
        userMapper.delete(ids);
    }

    @Override
    public void updateUser(User user) {
        user.setUpdateTime(LocalDateTime.now());
        String password=user.getUsername()+"123";
        user.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
        userMapper.updateUser(user);
    }

    @Override
    public User findById(Integer id) {
        return userMapper.findById(id);
    }

    @Override
    public List<User> findUser(String roleLabel) {
        return userMapper.findList(roleLabel);
    }

    @Override
    public LoginResultVo login(User user) {
        User u= userMapper.selectByUserName(user.getUsername());
        if(u==null){
            log.info("用户不存在");
            throw new ServiceException("用户名或密码错误");
        }
        String password=DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        if(!password.equals(u.getPassword())){
            log.info("密码错误");
            throw new ServiceException("用户名或密码错误");
        }

        if(u.getStatus()!=1){
            log.info("已停用");
            throw new ServiceException("已停用");
        }

        LoginResultVo loginResultVo=new LoginResultVo();
        loginResultVo.setId(u.getId());
        loginResultVo.setUsername(u.getUsername());
        loginResultVo.setName(u.getName());
        loginResultVo.setImage(u.getImage());
        loginResultVo.setRoleLabel(u.getRoleLabel());
        loginResultVo.setToken("");

        Map<String,Object> claims=new HashMap<>();
        claims.put("id",u.getId());
        claims.put("username",u.getUsername());
        String jwt= JwtUtils.generateToken(claims);
        loginResultVo.setToken(jwt);
        return loginResultVo;
    }

    @Override
    public List<User> findByDeptId(Integer deptId) {
        return userMapper.selectByDeptId(deptId);
    }

    @Override
    public List<User> selectAll() {
        return userMapper.selectAll();
    }


}
